import formatTime from '../../utils/util.js'
wx.cloud.init({
  env:"pethub-database"
})

const db = wx.cloud.database().collection("user")
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  info:{},
  openid:'',
  _id:'',
  isshow:{ 'richang': [], 'toubu': [], 'xiabanshen': [], 'qita': [] },
  onLoad: function (opt) {
    var date = new Date()
    var today = formatTime.formatTime(date).split(" ")[0]
    date.setFullYear(date.getFullYear() - 1)
    this.openid = opt.openid
    // 参数无效表示用户首次登录
    if (!opt.openid) {
      var form = wx.getStorageSync("form")
      if(form["pet"]=="dog"){
        this.isshow['richang'] = [1,4]
        this.isshow['toubu'] = [1]
        this.isshow['xiabanshen'] = [0,1]
        this.isshow['qita'] = []
        this.info = {
          richang: [
            {
              id: 1,
              name: '喂食',
              isshow: false,
              date: today,
              slider: '3',
              datechange: 'weishi',
              min: '0',
              max: '15'
            },
            {
              id: 2,
              name: '驱虫',
              isshow: true,
              date: today,
              slider: '30',
              datechange: 'quchong',
              min: '0',
              max: '100'
            },
            {
              id: 3,
              name: '护毛发',
              isshow: false,
              date: today,
              slider: '30',
              datechange: 'maofa',
              min: '0',
              max: '100'
            },
            {
              id: 4,
              name: '喝水',
              isshow: false,
              date: today,
              slider: '3',
              datechange: 'heshui',
              min: '0',
              max: '15'
            },
            {
              id: 5,
              name: '洗澡',
              isshow: true,
              date: today,
              slider: '14',
              datechange: 'xizao',
              min: '0',
              max: '60'
            }
          ],
          toubu: [
            {
              id: 1,
              name: '清眼睛',
              isshow: false,
              date: today,
              slider: '1',
              datechange: 'yanjing',
              min: '0',
              max: '30'
            },
            {
              id: 2,
              name: '清牙齿',
              isshow: true,
              date: today,
              slider: '4',
              datechange: 'yachi',
              min: '0',
              max: '60'
            },
            {
              id: 3,
              name: '清耳道',
              isshow: false,
              date: today,
              slider: '10',
              datechange: 'erdao',
              min: '0',
              max: '100'
            }
          ],
          xiabanshen: [
            {
              id: 1,
              name: '剪指甲',
              isshow: true,
              date: today,
              slider: '10',
              datechange: 'zhijia',
              min: '0',
              max: 100
            },
            {
              id: 2,
              name: '挤肛门',
              isshow: true,
              date: today,
              slider: '45',
              datechange: 'gangmen',
              min: '0',
              max: '100'
            },
            {
              id: 3,
              name: '护脚掌',
              isshow: false,
              date: today,
              slider: '8',
              datechange: 'jiaozhang',
              min: '0',
              max: '30'
            }
          ],
          qita: [
            {
              id: 1,
              name: '打疫苗',
              isshow: false,
              date: today,
              slider: '30',
              datechange: 'yimiao',
              min: '0',
              max: '180'
            },
            {
              id: 2,
              name: '怀孕',
              isshow: false,
              date: today,
              slider: '60',
              datechange: 'huaiyun',
              min: '0',
              max: '120'
            },
            {
              id: 3,
              name: '溜达',
              isshow: false,
              date: today,
              slider: '30',
              datechange: 'liuda',
              min: '0',
              max: '100'
            },
            {
              id: 4,
              name: '月经',
              isshow: false,
              date: today,
              slider: '180',
              datechange: 'yuejing',
              min: '0',
              max: '365'
            }
          ]
        }
      }
      else{
        this.isshow['richang'] = [1, 4]
        this.isshow['toubu'] = [1]
        this.isshow['xiabanshen'] = [0]
        this.isshow['qita'] = [0]
        this.info = {
          richang: [
            {
              id: 1,
              name: '喂食',
              isshow: false,
              date: today,
              slider: '1',
              datechange: 'weishi',
              min: '0',
              max: '15'
            },
            {
              id: 2,
              name: '驱虫',
              isshow: true,
              date: today,
              slider: '60',
              datechange: 'quchong',
              min: '0',
              max: '100'
            },
            {
              id: 3,
              name: '护毛发',
              isshow: false,
              date: today,
              slider: '2',
              datechange: 'maofa',
              min: '0',
              max: '100'
            },
            {
              id: 4,
              name: '喝水',
              isshow: false,
              date: today,
              slider: '1',
              datechange: 'heshui',
              min: '0',
              max: '15'
            },
            {
              id: 5,
              name: '洗澡',
              isshow: true,
              date: today,
              slider: '35',
              datechange: 'xizao',
              min: '0',
              max: '60'
            }
          ],
          toubu: [
            {
              id: 1,
              name: '清眼睛',
              isshow: false,
              date: today,
              slider: '1',
              datechange: 'yanjing',
              min: '0',
              max: '30'
            },
            {
              id: 2,
              name: '清牙齿',
              isshow: true,
              date: today,
              slider: '7',
              datechange: 'yachi',
              min: '0',
              max: '60'
            },
            {
              id: 3,
              name: '清耳道',
              isshow: false,
              date: today,
              slider: '1',
              datechange: 'erdao',
              min: '0',
              max: '100'
            }
          ],
          xiabanshen: [
            {
              id: 1,
              name: '剪指甲',
              isshow: true,
              date: today,
              slider: '14',
              datechange: 'zhijia',
              min: '0',
              max: 100
            },
            {
              id: 3,
              name: '护脚掌',
              isshow: false,
              date: today,
              slider: '14',
              datechange: 'jiaozhang',
              min: '0',
              max: '30'
            }
          ],
          qita: [
            {
              id: 1,
              name: '打疫苗',
              isshow: true,
              date: today,
              slider: '30',
              datechange: 'yimiao',
              min: '0',
              max: '180'
            },
            {
              id: 2,
              name: '怀孕',
              isshow: false,
              date: today,
              slider: '60',
              datechange: 'huaiyun',
              min: '0',
              max: '120'
            },
            {
              id: 3,
              name: '溜达',
              isshow: false,
              date: today,
              slider: '30',
              datechange: 'liuda',
              min: '0',
              max: '100'
            }
          ]
        }
      }
      this.setData({
        richang: this.info['richang'],
        toubu: this.info['toubu'],
        xiabanshen: this.info['xiabanshen'],
        qita: this.info['qita'],
        first:true,
        end: formatTime.formatTime(new Date()).split(" ")[0],
        start: formatTime.formatTime(date).split(" ")[0],
        form:wx.getStorageSync("form")
      })
    }

    // 请求参数有效就不是首次使用了
    else{
      db.where({_openid:opt.openid}).get().then(res=>{
        this.info['richang'] = res.data[0]['richang']
        this.info['toubu'] = res.data[0]['toubu']
        this.info['xiabanshen'] = res.data[0]['xiabanshen']
        this.info['qita'] = res.data[0]['qita']
        this._id = res.data[0]["_id"]
        this.setData({
          richang: this.info['richang'],
          toubu: this.info['toubu'],
          xiabanshen: this.info['xiabanshen'],
          qita: this.info['qita'],
          first: false,
          end: formatTime.formatTime(new Date()).split(" ")[0],
          start: formatTime.formatTime(date).split(" ")[0],
          form: res.data[0]
        })
        var field = ['richang', 'toubu', 'xiabanshen', 'qita']
        // 及时记录可显示条目
        this.isshow = wx.getStorageSync("isshow")
      })
    }
    console.log("onload  "+this.isshow)
  },

  change: function (field,value) {
    if(field=="richang"){
      this.setData({
        richang: value
      })
    }
    else if(field=="toubu"){
      this.setData({toubu:value})
    }
    else if (field =="xiabanshen"){
      this.setData({xiabanshen:value})
    }
    else if(field=="qita"){
      this.setData({qita:value})
    }
  },

  selected: function (e) { 
    var d = e.currentTarget.dataset
    // 是否隐藏的设置
    const that = this
    if (d.subfield=="isshow"){
      if (that.isshow[d.field].includes(d.rindex)){
        that.isshow[d.field].splice(
          that.isshow[d.field].indexOf(d.rindex),
          1
        )
      }
      else{
        that.isshow[d.field].push(d.rindex)
      }
      that.info[d.field][d.rindex][d.subfield] = !that.info[d.field][d.rindex][d.subfield]
    }
    // 时间节点设置
    else{
      that.info[d.field][d.rindex][d.subfield] = e.detail.value
    }
    that.change(d.field, that.info[d.field])
    
  },
  
  reset: function(e){
    this.setData({
      richang: this.data['richang'],
      toubu: this.data['toubu'],
      xiabanshen: this.data['xiabanshen'],
      qita: this.data['qita'],
      first: true
    })
  },

  submit:function(e){
    // 及时更新显示隐藏缓存
    wx.setStorageSync("isshow", this.isshow)
    var f = wx.getStorageSync("form")
    console.log("submitted  "+this.isshow)
    // 没有openid说明是首次登录
  if(!this.openid){
    wx.setStorageSync("lastTime", formatTime.formatTime(new Date()).split(" ")[0])
    db.add({
      data: {
        name:f.name,
        pet:f.pet,
        gender:f.gender,
        age:f.age,
        richang: this.info['richang'],
        toubu: this.info['toubu'],
        xiabanshen: this.info['xiabanshen'],
        qita: this.info['qita'],
        records:[]
        }
    // 担心还未将数据添加到数据库时，就跳转到了index页检测到无这个openid又跳转至初次加载页这种致命的问题。
    // then保证添加到数据库后再跳转至index页
    }).then(
      wx.reLaunch({ url: '/pages/index/index' }))
  }
  // 二次登录，已经存有记录
  else {
    db.doc(this._id).update({
      data: {
        name: e.detail.value.name,
        richang: this.info['richang'],
        toubu: this.info['toubu'],
        xiabanshen: this.info['xiabanshen'],
        qita: this.info['qita']}
    }).then(
      wx.reLaunch({ url: '/pages/index/index' }))
  }
  
  }  
})